package com.sky.service.UserService.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.ShoppingCart;
import com.sky.service.UserService.UserShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class UserShoppingCartlmpl implements UserShoppingCartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    DishMapper dishMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int ShoppingCartadd(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //判断客户下单的是 套餐 setmeal_id 还是菜品 dish_id
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            // 进行判断是第一次下单 还是增加订单
            ShoppingCart allShoppingCart = shoppingCartMapper.selectShoppingCart(currentId, shoppingCartDTO);
            if (Objects.isNull(allShoppingCart)) {
                shoppingCart.setNumber(1);
                // 根据DishId查询到菜品的信息
                Dish dishId = dishMapper.getByIdDish(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dishId.getPrice());
                shoppingCart.setName(dishId.getName());
                shoppingCart.setImage(dishId.getImage());
                int i = shoppingCartMapper.ShoppingCartadd(shoppingCart);
                log.warn("插入的新菜品:{}", i);
            } else {
                //对下单的数量number进行新增 直接去修改数据库的number数据
                int i = shoppingCartMapper.updateDishNumber(shoppingCartDTO.getDishId());
                log.warn("修改后的数据:{}", i);
            }


        }//如果是套餐 setmeal_id
        else {
            if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
                // 进行判断是第一次下单 还是增加订单
                ShoppingCart allShoppingCart = shoppingCartMapper.selectShoppingCart(currentId, shoppingCartDTO);
                if (Objects.isNull(allShoppingCart)) {
                    shoppingCart.setNumber(1);
                    // 根据DishId查询到菜品的信息
                    Dish dishId = dishMapper.getByIdSetmeal(shoppingCartDTO.getSetmealId());
                    shoppingCart.setAmount(dishId.getPrice());
                    shoppingCart.setName(dishId.getName());
                    shoppingCart.setImage(dishId.getImage());
                    int i = shoppingCartMapper.ShoppingCartadd(shoppingCart);
                    log.warn("插入的新菜品:{}", i);
                } else {
                    //对下单的数量number进行新增 直接去修改数据库的number数据
                    int i = shoppingCartMapper.updatesetmealNumber(shoppingCartDTO.getSetmealId());
                    log.warn("修改后的数据:{}", i);
                }
            }
        }
        return 1;
    }

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        List<ShoppingCart> list = shoppingCartMapper.list();
        return list;
    }

    @Override
    public void deleteShoppingCart() {
        shoppingCartMapper.deleteShoppingCart();
    }
}
